﻿namespace PorteringManagement.Model
{
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;

    public class PortRelation
    {
        [Key]
        public long PortRelationID { get; set; }

        public int OriginPortID { get; set; }

        public int DestinationPortID { get; set; }

        public virtual Port OriginPort { get; set; }

        public virtual Port DestinationPort { get; set; }

        public int TimeWeight { get; set; }

        public int WeatherWeight { get; set; }

        public int CostWeight { get; set; }

        /// <summary>
        /// Returns all the Port Relations that have any of this ports as origin or destination.
        /// </summary>
        public static IEnumerable<PortRelation> GetAllByPorts(IEnumerable<Port> ports)
        {
            var context = new PorteringManagementContext();
            var portIds = ports.Select(p => p.PortID);

            return context.PortRelations
                .Where(pr => portIds.Any(id => (id == pr.OriginPortID) || (id == pr.DestinationPortID)))
                .ToList();
        }

        public int GetWeight(AnalysisType analysisType)
        {
            switch (analysisType)
            { 
                case AnalysisType.Time:
                    return this.TimeWeight;
                case AnalysisType.Cost:
                    return this.CostWeight;
                case AnalysisType.Weather:
                    return this.WeatherWeight;
            }

            return int.MaxValue;
        }
    }
}
